package io.github.hellomaker.domain.strategy.model.entity;

import io.github.hellomaker.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author hellomaker
 *策略规则实体
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {

    /**
     * 抽奖策略ID
     */
    private Long strategyId;
    /**
     * 抽奖奖品ID【规则类型为策略，则不需要奖品ID】
     */
    private Long awardId;
    /**
     * 抽象规则类型；1-策略规则、2-奖品规则
     */
    private Integer ruleType;
    /**
     * 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】
     */
    private String ruleModel;
    /**
     * 抽奖规则比值
     */
    private String ruleValue;
    /**
     * 抽奖规则描述
     */
    private String ruleDesc;

    public String[] listRuleValues() {
        if (StringUtils.isEmpty(ruleValue)) {
            return new String[0];
        }
        return ruleValue.split(Constants.SPACE);
    }

    public Map<Integer, List<Integer>> getRuleWeightValues() {
        String[] ruleValues = listRuleValues();
        if (ruleValues == null || ruleValues.length == 0) {
            return null;
        }
        Map<Integer, List<Integer>> ruleWeightValues = new HashMap<>();
        for (String ruleValue : ruleValues) {
            String[] ruleValueItem = ruleValue.split(Constants.COLON);
            String[] awardIdList = ruleValueItem[1].split(Constants.SPLIT);
            ruleWeightValues.put(Integer.valueOf(ruleValueItem[0]), Stream.of(awardIdList).map(Integer::new).collect(Collectors.toList()));
        }
        return ruleWeightValues;
    }


}
